#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
using namespace std;
typedef long long ll;
bool cmp(pair<int, int> a, pair<int, int> b)
{
	return a.second > b.second;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n = 0;

		string s;
		pair<int, int> tank[26];
		for (int i = 0; i < 26; i++)
		{
			tank[i].first = i;
			tank[i].second = 0;
		}
		cin >> n >> s;
		int num = 0;
		for (int i = 0; i < s.size(); i++)
		{
			if (tank[s[i] - 'a'].second == 0)
			{
				num++;
			}
			tank[s[i] - 'a'].second++;
		}
		sort(tank, tank + 26, cmp);
		int flag = 1;
		for (int i = 0; i < num - 1; i++)
		{
			if (tank[i].second != tank[i + 1].second)
			{
				flag = 0;
				break;
			}
		}
		if (flag)
		{
			cout << 0 << endl;
			cout<<s<<endl;
			continue;
		}
		int ans = n;
		int x = 0;
		int ind = 0;
		for (int i = 1; i <= min(26, n); i++)
		{
			if (n % i)
			{
				continue;
			}
			int l = 0, r = 0;
			x = n / i;
			for (int j = 0; j < i; j++)
			{
				if (tank[j].second > x)
				{
					r += tank[j].second - x;
				}
				else if (tank[j].second < x)
				{
					l += x - tank[j].second;
				}

			}
			if (ans > max(l, r))
			{
				ind = i;
				ans = max(l, r);
			}

		}
		cout << ans << endl;
		int b[26] = { 0 };
		int have[26] = { 0 };
		string add;
		for (int i = 0; i < ind; i++)
		{
			b[tank[i].first] = tank[i].second;
			if (b[tank[i].first] < n / ind)
			{
				for (int j = b[tank[i].first]; j < n / ind; j++)
				{
					add += char(tank[i].first + 'a');
				}
			}
			
		}
		int idx = 0;
		for (int i = 0; i < n; i++)
		{
			if (b[s[i] - 'a'] == 0 || have[s[i] - 'a'] == n / ind)
			{
				cout << add[idx++];
			}
			else
			{
				cout << s[i];
				have[s[i] - 'a']++;
			}
		}
		cout << endl;
	}


	return 0;
}

